﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Aulac.Verb.Ems;
using Aulac.Global.Xml;
using Aulac.Rsc.Bss.Tags;
using Aulac.Rsc;
using Aulac.Global.Enums;
using Aulac.Client.Global.Interface;
using Aulac.Global.Common;
using System.Data;
using Aulac.Global.Data;
using Aulac.Verb.Bss;
using Aulac.Rsc.Bss;
using System.Windows.Forms;
using Aulac.Client.Data.XmlQuery;

namespace Aulac.Client.Bss.InOutPut
{
    partial class FormVchBuyCapAss
    {
        private bool GetDetailVchBuyCapAss()
        {
            try
            {
                // Lay chi tiet cua mot phieu.
                XmlDataDocument xmlDoc = DataRequest.CreateXmlDocument(AulacBssVerb.BuyCapAss.GetDetailBuyCapAss, AulacBssVerb.BuyCapAss.Noun.BuyCapAss);
                XmlProcess.AddValueParamItem(xmlDoc, tagVch.VchBuyHeaderItem.VchBuyCapAssPrkID, grdInfo.GetValue(grdInfo.SelectedGridRowView, tagVch.VchBuyHeaderItem.VchBuyCapAssPrkID));
                XmlDataDocument outDoc = DataRequest.ProcessRequest(xmlDoc);

                // Dua du lieu len Form
                XmlElement eHeader = XmlProcess.GetVchHeaderElm(outDoc);
                List<IAulacDataControl> mLstControls = null;
                GetListAulacDataControls(ref mLstControls, grpParner);
                mLstControls.AddRange(new List<IAulacDataControl> { txtVchNo, dtVchDate });
                foreach (IAulacValueDataCtrl ctr in mLstControls)
                    ctr.SetValueToControl(eHeader);

                XmlElement eVLines = XmlProcess.GetVchLinesElm(outDoc);
                grdLine.ClearDataContainer();                 
                if (!ValueHelper.isEmpty(eVLines))
                    ((IAulacDataContainerCtrl)grdLine).AddDataInParentNodeToControl(eVLines);
                VchBuyCapAssPrkID = XmlProcess.GetValueVchHeader(outDoc, tagVch.VchBuyHeaderItem.VchBuyCapAssPrkID);
                
                return true;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }            
        }
        
        private bool SaveVchBuyCapAss(ref XmlDataDocument xmlData)
        {
            try
            {
                // Cái gì anh noi thi em phai lam chu.
                // Cai grd cua em một là trong Ds không có trường VchBuyCapAssPrkID, Hai là em Load dữ liệu cho Grd Ko có trường đó.
                // Thử lại xem.
               // Khi nào VchBuyCapAss có giá trị mới Update dc. Chứ ko nó biết dòng nào mà Update.
                bool isEdit = (FormMode == FormModes.EditMode) ? true : false;
                string sVerb = (!isEdit) ? Verb.Bss.AulacBssVerb.BuyCapAss.AddVchBuyCapAss : Verb.Bss.AulacBssVerb.BuyCapAss.UpdateVchBuyCapAss;
                ClientExecXmlQueryData query = new ClientExecXmlQueryData();
                query.SetRequestInfo(sVerb, Verb.Bss.AulacBssVerb.BuyCapAss.Noun.BuyCapAss);
                XmlElement eHeader = query.SetVchHeaderElm();
                foreach (Aulac.Client.Global.Interface.IAulacValueDataCtrl ctr in GetListControlsOnForm())
                    ctr.GetValueToXml(ref eHeader);
                XmlProcess.SetAtrb(ref eHeader, tagVch.VchBuyHeaderItem.VchName, mVchName);
                string s = cboMngStaff.StaffPrkID;
                XmlProcess.SetAtrb(ref eHeader, tagVch.VchBuyHeaderItem.VchTypeID, mVchType);
                if (!isEdit)
                    query.ObjQueryVchHeader.AddFieldPrk(tagVch.VchBuyHeaderItem.VchBuyCapAssPrkID, DbType.Decimal, KeyTypes.AutoNumber);
                else
                {
                    XmlProcess.SetAtrb(ref eHeader, tagVch.VchBuyHeaderItem.VchBuyCapAssPrkID, VchBuyCapAssPrkID);
                    query.ObjQueryVchHeader.AddValueParamFilterItem(tagVch.VchBuyHeaderItem.VchBuyCapAssPrkID, tagVch.VchBuyHeaderItem.VchBuyCapAssPrkID, VchBuyCapAssPrkID);
                }

                XmlElement eVLines = query.SetVchLinesElm();
                int iCountLine = grdLine.grdData.ReadOnly == true ? grdLine.grdData.Rows.Count : grdLine.grdData.Rows.Count - 1;
                for (int i = 1; i < iCountLine; i++)
                    grdLine.grdData.SetValueCell(i, tagVch.VchBuyLineItem.ItemLine, i);
                ((IAulacDataContainerCtrl)grdLine).GetDataToChildNodeXml(eVLines);
                //XmlNodeList mNodeList = XmlProcess.
                //foreach (XmlElement Elmt in mNodeList)
                //{
                //    XmlElement eL = Elmt;
                    
                //    string sAmtLineInExc = eL.Attributes[tagVch.VchBuyLineCapAss.AmtLineInExc].Value.ToString().Replace(",", ".");
                //    string sAmtTaxLineInExc = eL.Attributes[tagVch.VchBuyLineCapAss.AmtTaxLineInExc].Value.ToString().Replace(",", ".");
                //    string sVAT = eL.Attributes[tagVch.VchBuyLineCapAss.VAT].Value.ToString().Replace(",",".");
                //    string sUnitPrice = eL.Attributes[tagVch.VchBuyLineCapAss.UnitPrice].Value.ToString().Replace(",",".");

                //    XmlProcess.SetAtrb(ref eL, tagVch.VchBuyLineCapAss.AmtLineInExc, sAmtLineInExc);
                //    XmlProcess.SetAtrb(ref eL, tagVch.VchBuyLineCapAss.AmtTaxLineInExc, sAmtTaxLineInExc);
                //    XmlProcess.SetAtrb(ref eL, tagVch.VchBuyLineCapAss.VAT, sVAT);
                //    XmlProcess.SetAtrb(ref eL, tagVch.VchBuyLineCapAss.UnitPrice, sUnitPrice);
                //}
                xmlData = query.ExecureQueryData();
                return true;
            }
            catch (Exception ex)
            {
                Aulac.Global.Exceptions.AulacException x = new Aulac.Global.Exceptions.AulacException(ex);
                if (ErrCodes.ErrorCode.Error0010.Equals(x.ErrorCode))
                {
                    XmlNodeList lst = XmlProcess.GetListVchLineItem(x.XmlEcexption);
                    foreach (XmlElement eLine in lst)
                    {
                        DataRow row = grdLine.AulacDataGrid.GetFirstDataRowByKey(tagVch.VchBuyLineItem.ItemPrkID, XmlProcess.GetValueAtrb(eLine, tagVch.VchBuyLineItem.ItemPrkID));
                    }
                    return false;
                }
                else throw ex;

            }
        }

        private bool DelVchBuyCapAss()
        {
            try
            {
                mIsCheckInstock = true;
                XmlDataDocument xmlDoc = CreateXmlDocument(Verb.Bss.AulacBssVerb.BuyCapAss.DelVchBuyCapAss, Verb.Bss.AulacBssVerb.BuyCapAss.Noun.BuyCapAss);
                XmlProcess.AddValueParamItem(xmlDoc, tagVch.VchBuyHeaderItem.VchBuyCapAssPrkID, grdInfo.GetValue(grdInfo.SelectedGridRowView, tagVch.VchBuyHeaderItem.VchBuyCapAssPrkID));
                ProcessRequest(xmlDoc);
                return true;
            }
            catch (Exception Ex)
            {
                Aulac.Global.Exceptions.AulacException x = new Aulac.Global.Exceptions.AulacException(Ex);
                if (ErrCodes.ErrorCode.Error0010.Equals(x.ErrorCode))
                {
                    XmlNodeList lst = XmlProcess.GetListVchLineItem(x.XmlEcexption);
                    foreach (XmlElement eLine in lst)
                    {
                    }
                    return false;
                }
                else throw Ex;
            }            
        }
        private string GetNewVchNo(ref string pVchName)
        {
            XmlDataDocument xmlDoc = DataRequest.CreateXmlDocument(AulacBssVerb.BuyCapAss.GetNewVchNo, AulacBssVerb.BuyCapAss.Noun.BuyCapAss);
            XmlProcess.AddValueParamItem(xmlDoc, tagVch.VchBuyHeaderItem.VchTypeID, mVchType);
            XmlDataDocument outDoc = DataRequest.ProcessRequest(xmlDoc);
            pVchName = XmlProcess.GetValueVchHeader(outDoc, tagVch.VchBuyHeaderItem.VchName);
            return XmlProcess.GetValueVchHeader(outDoc, tagVch.VchBuyHeaderItem.VchNo);
        }
    }
}
